package com.yuebu.yuebubtlib;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import com.yuebu.yuebubtlib.exception.BLEException;
import com.yuebu.yuebubtlib.util.BLELogUtil;

/**
 * BLEScanner BLE蓝牙扫描回调类
 *
 * @author SJL
 * @date 2017/5/3
 */
@SuppressLint("NewApi")
public class BLEScanner implements BluetoothAdapter.LeScanCallback {
    private static final String TAG = "BLEScanner";
    private Context mContext;
    private boolean isScanning = false;
    //超时时间
    private int timeout = 10000;
    //超时处理
    private Handler timeoutHandler = new Handler(Looper.getMainLooper());
    //扫描回调
    private OnBLEScanListener onBLEScanListener;
    private BluetoothAdapter bluetoothAdapter;


    public interface OnBLEScanListener {
        void onScanResult(BluetoothDevice device, int rssi, byte[] scanRecord);

        void onScanFailed(BLEException bleException);
    }

    public BLEScanner(Context context, OnBLEScanListener onBLEScanListener) {
        this.mContext = context;
        this.onBLEScanListener = onBLEScanListener;
        init(context);
    }

    public BLEScanner(int timeout, Context context, OnBLEScanListener onBLEScanListener) {
        this.timeout = timeout;
        this.mContext = context;
        this.onBLEScanListener = onBLEScanListener;
        init(context);
    }

    private void init(Context context) {
        if (bluetoothAdapter == null) {
            BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
            bluetoothAdapter = bluetoothManager.getAdapter();
        }
    }

    /**
     * 开始扫描
     */
    public void startScan() {
        BLELogUtil.e(TAG, "startScan");
        if (bluetoothAdapter == null) {
            return;
        }
        bluetoothAdapter.startLeScan(this);

        isScanning = true;
        if (timeout > 0) {
            timeoutHandler.postDelayed(mRunnable, timeout);
        }
    }

    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            scanTimeOut();
        }
    };

    /**
     * 扫描超时
     */
    private void scanTimeOut() {
        BLELogUtil.e(TAG, "scanTimeOut");
        stopScan();
        onBLEScanListener.onScanFailed(new BLEException(BLEException.SCAN_TIMEOUT));
    }

    /**
     * 停止扫描
     */
    public void stopScan() {
        BLELogUtil.e(TAG, "stopScan");
        isScanning = false;
        timeoutHandler.removeCallbacks(mRunnable);
        if (bluetoothAdapter == null) {
            return;
        }
        try {
            bluetoothAdapter.stopLeScan(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onLeScan(BluetoothDevice bluetoothDevice, int i, byte[] bytes) {
        onBLEScanListener.onScanResult(bluetoothDevice, i, bytes);
    }

    public OnBLEScanListener getOnBLEScanListener() {
        return onBLEScanListener;
    }

    public void setOnBLEScanListener(OnBLEScanListener onBLEScanListener) {
        this.onBLEScanListener = onBLEScanListener;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }


}
